package xing2387.filtervideoplayer.player

import android.net.Uri
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.ExoPlayerFactory
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import xing2387.filtervideoplayer.App
import xing2387.filtervideoplayer.R


class GLPlayer {

    companion object {
        val sInstance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            GLPlayer()
        }
    }

    val exoPlayer: SimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(App.sAppContext)

    init {
    }

    private val dataSourceFactory by lazy {
        DefaultDataSourceFactory(App.sAppContext, App.sAppContext.getString(R.string.app_name))
    }

    fun startVideo(videoUrl: String) {
        val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(Uri.parse(videoUrl))
        exoPlayer.prepare(mediaSource)
        exoPlayer.playWhenReady = true;
    }

    fun pauseVideo() {
        exoPlayer.playWhenReady = false
    }

    fun resumeVideo() {
        exoPlayer.playWhenReady = true
    }

    fun isPlaying(): Boolean {
        return exoPlayer.isPlaying
    }
}